package com.gwz.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwz.sys.constast.SYSConstast;
import com.gwz.sys.service.PermissionService;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.utils.ResultObj;
import com.gwz.sys.vo.PermissionVo;

@RestController
@RequestMapping("permission")
public class PermissionController {

	@Autowired
	private PermissionService permissionService;
	
	@RequestMapping("loadAllPermission")
	public DataGridView loadAllPermission(PermissionVo permissionVo) {
		permissionVo.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		permissionVo.setType(SYSConstast.PERMISSION_TYPE_PERMISSION);
		return permissionService.queryAllPermission(permissionVo);
	}
	

	@RequestMapping("loadMaxOrderNumber")
	public Integer loadMaxOrderNumber() {
		Integer num=this.permissionService.queryMaxOrderNumber();
		return num+1;
	}
	
	
	@RequestMapping("addPermission")
	public ResultObj addPermission(PermissionVo permissionVo) {
		ResultObj resultObj = new ResultObj(200, "添加成功");
		try {
			permissionVo.setType(SYSConstast.PERMISSION_TYPE_PERMISSION);
			permissionService.addPermission(permissionVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("添加失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	
	@RequestMapping("updatePermission")
	public ResultObj updatePermission(PermissionVo permissionVo) {
		ResultObj resultObj = new ResultObj(200, "修改成功");
		try {
			permissionService.updatePermission(permissionVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("修改失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	
	@RequestMapping("isCurrentPermissionHasChildrenNode")
	public ResultObj isCurrentPermissionHasChildrenNode(Integer id) {
		Integer childrenCount=this.permissionService.isCurrentPermissionHasChildrenNode(id);
		if(childrenCount>0) {
			return new ResultObj(-1, "当前权限有子权限，请先删除子权限");
		}else {
			return new ResultObj(200, "");
		}
	}
	
	
	@RequestMapping("deletePermission")
	public ResultObj deletePermission(PermissionVo permissionVo) {
		ResultObj resultObj = new ResultObj(200, "删除成功");
		try {
			permissionService.deletePermission(permissionVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("删除失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	

}
